package com.example.administrator.newsdemo.cn.gdcp.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.administrator.newsdemo.ContentActivity;
import com.example.administrator.newsdemo.NewsBean;
import com.example.administrator.newsdemo.R;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/5/7 0007.
 */

public class HeadlineFragment extends Fragment {
    private RecyclerView my_rv;
    private List<NewsBean.ResultBean.DataBean> dataBeans;
    private MyAdapter adapter;
    private Handler handler;
    private static final int RESULT = 1001;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_view,container,false);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        my_rv = view.findViewById(R.id.rv_resource);
        my_rv.setLayoutManager(manager);
        getResult();
        initHandler();
        return view;
    }

    private void initHandler() {
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if(msg.what == RESULT){
                    my_rv.setAdapter((RecyclerView.Adapter) msg.obj);
                }
                return false;
            }
        });
    }

    private void getResult() {
        String url = "http://v.juhe.cn/toutiao/index?type=&key=c357fe763c1c935a11c423123847abc8";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                NewsBean bean = new Gson().fromJson(response.body().string(),NewsBean.class);
                dataBeans = bean.getResult().getData();
                adapter = new MyAdapter(dataBeans);
                Message message = new Message();
                message.what = RESULT;
                message.obj = adapter;
                handler.sendMessage(message);
            }
        });
    }
}
